package com.ruoyi.web.controller.community.cpc;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.community.domain.CpcActivity;
import com.ruoyi.community.domain.CpcActivityEnroll;
import com.ruoyi.community.service.ICpcActivityEnrollService;
import com.ruoyi.community.service.ICpcActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/community/cpc/enroll")
public class CpcActivityEnrollController extends BaseController {
    @Autowired
    private ICpcActivityEnrollService CpcActivityEnrollService;

    @Autowired
    private ICpcActivityService CpcActivityService;

    /**
     * 查询活动报名列表
     */
//    @PreAuthorize("@ss.hasPermi('community:enroll:list')")
    @GetMapping("/list")
    public TableDataInfo listEnroll(CpcActivityEnroll CpcActivityEnroll)
    {
        startPage();
        List<CpcActivityEnroll> list = CpcActivityEnrollService.selectCpcActivityEnrollList(CpcActivityEnroll);
        return getDataTable(list);
    }

    /**
     * 查询某人报名的所有活动
     */
    @GetMapping("/getMyEnroll")
    public AjaxResult listByUserId()
    {
        CpcActivityEnroll cpcActivityEnroll = new CpcActivityEnroll();
        cpcActivityEnroll.setUserId(SecurityUtils.getUserID());
        List<CpcActivityEnroll> list = CpcActivityEnrollService.selectCpcActivityEnrollList(cpcActivityEnroll);
        List<CpcActivity> resultList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            CpcActivity cpcActivity = CpcActivityService.selectCpcActivityById(list.get(i).getActivityId());
            resultList.add(cpcActivity);
        }
        return AjaxResult.success(resultList);
    }

    /**
     * 查询 活动报名记录 和 报名用户信息
     */
    @GetMapping("/list/{activityId}")
    public AjaxResult listEnrollById(@PathVariable("activityId") String activityId)
    {
//        List<SysUser> list = CpcActivityEnrollService.selectCpcActivityEnrollListById(activityId);

        List<SysUser> userList = CpcActivityEnrollService.selectCpcActivityEnrollListById(activityId);
        CpcActivityEnroll query = new CpcActivityEnroll();
        query.setActivityId(activityId);
        List<CpcActivityEnroll> enrollList = CpcActivityEnrollService.selectCpcActivityEnrollList(query);
        ArrayList<Object> list = new ArrayList<>();
        for (int i = 0; i < enrollList.size(); i++) {
            Map enrollDict = new HashMap<>();
            enrollDict.put("record", enrollList.get(i));
            enrollDict.put("userInfo", userList.get(i));
            list.add(enrollDict);
        }
        return AjaxResult.success(list);
    }


    /**
     * 导出活动报名详情列表
     */
//    @PreAuthorize("@ss.hasPermi('community:enroll:export')")
    @Log(title = "活动报名详情", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult exportEnroll(CpcActivityEnroll CpcActivityEnroll)
    {
        List<CpcActivityEnroll> list = CpcActivityEnrollService.selectCpcActivityEnrollList(CpcActivityEnroll);
        ExcelUtil<CpcActivityEnroll> util = new ExcelUtil<CpcActivityEnroll>(CpcActivityEnroll.class);
        return util.exportExcel(list, "活动报名详情数据");
    }

    /**
     * 报名
     */
//    @PreAuthorize("@ss.hasPermi('community:activityDetail:add')")
    @Log(title = "活动详情", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CpcActivityEnroll CpcActivityEnroll)
    {
        return toAjax(CpcActivityEnrollService.insertCpcActivityEnroll(CpcActivityEnroll));
    }
}
